<?php

namespace app\model\v1\recruit;

use crmeb\traits\ModelTrait;
use think\Model;

class RecruitFirmType extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];






    public static function getRecruitFirmTypeData($id = 0)
    {
        $result = [];
        $item = self::whereNull("delete_time")->where('pid',$id)->select()->toArray();
        if ($item){
            foreach($item as $k=> $v) {
                $item[$k]['children'] = [];
                $data = self::getRecruitFirmTypeData($v['id']);
                if ($data){
                    foreach($data as $kk=> $vs) {
                        $item[$k]['children'][$kk] = $vs;
                    }
                }
                $result[$k] =$item[$k];
            }
        }
        return $result;
    }


    /**
     * 求职招聘后台 - 企业类型管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRecruitFirmTypeList($param = [])
    {
        $data["data"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time, 
            case when status=1 then '启用' 
                when status=2 then '禁用' 
                end as status_text")
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("name",'like', '%'.$param["word"].'%');
                if(!empty($param["pid"])) $query->where("pid",  $param["pid"]);
            })
            ->order("id", "desc")
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->whereNull("delete_time")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("name",'like', '%'.$param["word"].'%');
                if(!empty($param["pid"])) $query->where("pid",  $param["pid"]);
            })
            ->count();

        if(!empty($data["data"])){
            foreach ($data["data"] as $key => &$val){
                $val["pid_name"] = self::getPidName($val["pid"]);
            }
        }

        return $data;
    }


    public static function getPidName($pid)
    {
        $arr = [];
        $parent = self::whereNull("delete_time")->where("id", $pid)->find();
        if(!empty($parent)){
            $arr[] = $parent['name'] ?? '';
            if($parent["pid"] > 0){
                $parent2 = self::where("id", $parent["pid"])->find();
                if(!empty($parent2)){
                    $arr[] = $parent2['name'] ?? '';
                    $parent3 = self::where("id", $parent2["pid"])->find();
                    if(!empty($parent3)){
                        $arr[] = $parent3['name'] ?? '';
                    }
                }
            }
        }
        return !empty($arr) ? implode("/", array_reverse($arr)) : '';
    }





    /**
     * 企业类型管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addRecruitFirmType($param = [])
    {
        $add = [
            "pid"               => $param["pid"],
            "name"              => $param["name"] ?? '',
            "sort"              => $param["sort"] ?? '',
            "create_time"       => time(),
        ];
        return self::insert($add);
    }


    /**
     * 企业类型管理列表 - 编辑
     * @param $param
     * @return RecruitIndustry
     */
    public static function updateRecruitFirmType($param = [])
    {
        $update = [];
        if(!empty($param["name"])) $update["name"] = $param["name"];
        if(!empty($param["pid"])) $update["pid"] = $param["pid"];
        if(!empty($param["sort"])) $update["sort"] = $param["sort"];

        return self::where("id", $param["id"])->update($update);
    }





    public static function enableRecruitFirmType($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }














}